/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.perfil.vista;

import basedatos.OperacionesBD;
import java.awt.Image;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import modulos.perfil.modelo.Perfil;
import vista.ClsInicio;
import vista.frmPrincipal;
import vista.modelo.*;

/**
 *
 * @author victor
 */
public class iFrmImgFondo extends javax.swing.JInternalFrame {

    ArrayList<Object> perfiles;

    /**
     * Creates new form iFrmImagenEmp
     */
    public iFrmImgFondo() {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        filtroImagen = new FileNameExtensionFilter("Archivos de imagen", "jpg", "png", "gif");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAceptar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnCargarImagenPrincipal = new javax.swing.JButton();
        lblPrincipal = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnCargarImagenInicio = new javax.swing.JButton();
        lblInicio = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtTextoBarraInferior = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtNombrePerfil = new javax.swing.JTextField();

        setClosable(true);
        setTitle("ESCRITORIO");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/entorno.png"))); // NOI18N
        setVisible(true);
        getContentPane().setLayout(null);

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/accept.png"))); // NOI18N
        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAceptar);
        btnAceptar.setBounds(220, 480, 100, 30);

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCerrar);
        btnCerrar.setBounds(450, 480, 93, 30);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Imagen de  Pantalla Principal", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Dialog", 1, 12), java.awt.SystemColor.activeCaption)); // NOI18N
        jPanel1.setLayout(null);

        btnCargarImagenPrincipal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/camera.png"))); // NOI18N
        btnCargarImagenPrincipal.setText("Cargar");
        btnCargarImagenPrincipal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarImagenPrincipalActionPerformed(evt);
            }
        });
        jPanel1.add(btnCargarImagenPrincipal);
        btnCargarImagenPrincipal.setBounds(55, 66, 129, 31);
        jPanel1.add(lblPrincipal);
        lblPrincipal.setBounds(275, 13, 240, 140);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 50, 532, 160);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Imagen de Inicio\n", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Dialog", 1, 12), java.awt.SystemColor.activeCaption)); // NOI18N
        jPanel2.setLayout(null);

        btnCargarImagenInicio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/camera.png"))); // NOI18N
        btnCargarImagenInicio.setText("Cargar");
        btnCargarImagenInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarImagenInicioActionPerformed(evt);
            }
        });
        jPanel2.add(btnCargarImagenInicio);
        btnCargarImagenInicio.setBounds(58, 65, 129, 31);
        jPanel2.add(lblInicio);
        lblInicio.setBounds(277, 13, 240, 140);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(10, 220, 530, 160);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Texto Barra Inferior ", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Dialog", 1, 12), java.awt.SystemColor.activeCaption)); // NOI18N
        jPanel3.setToolTipText("");
        jPanel3.setLayout(null);
        jPanel3.add(txtTextoBarraInferior);
        txtTextoBarraInferior.setBounds(40, 30, 444, 30);

        getContentPane().add(jPanel3);
        jPanel3.setBounds(10, 390, 530, 80);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel1.setText("Nombre Perfil:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 14, 110, 20);
        getContentPane().add(txtNombrePerfil);
        txtNombrePerfil.setBounds(130, 10, 350, 30);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-563)/2, (screenSize.height-553)/2, 563, 553);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        if (!txtNombrePerfil.getText().isEmpty() && lblInicio.getIcon() != null
                && lblPrincipal.getIcon() != null
                && !txtTextoBarraInferior.getText().equals("")) {
            perfil = new Perfil();
            perfil.setNombrePerfil(txtNombrePerfil.getText());
            perfil.setTextoBarra(txtTextoBarraInferior.getText());
            perfil.setImagenPantalla(convierteFoto(dirImagenFondo));
            perfil.setImagenInicio(convierteFoto(dirImagenInicio));
            perfil.setSeleccionado(true);
            if (OperacionesBD.guardar(perfil)) {
                ClsInicio.listar();
                ClsInicio.poneHabilitado(perfil.getNombrePerfil());
                Mensaje.datosGuardados();
            } else {
                Mensaje.datosNoGuardados();
            }
            frmPrincipal.dskpPrincipal.setUI(null);
            Image imagenFondo=new ImageIcon(dirImagenFondo.toString()).getImage();
            ((ImagenDesktopPane) frmPrincipal.dskpPrincipal).setImagen(imagenFondo);
            frmPrincipal.lblMensaje.setText(txtTextoBarraInferior.getText());
            dispose();
        } else {
            Mensaje.camposVacios();
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCargarImagenPrincipalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarImagenPrincipalActionPerformed
        JFileChooser jf = new JFileChooser();
        ImagePreviewPanel miniatura = new ImagePreviewPanel();
        jf.setAccessory(miniatura);
        jf.addPropertyChangeListener(miniatura);
        jf.setFileFilter(filtroImagen);
        int opc = jf.showOpenDialog(this);
        if (opc == JFileChooser.APPROVE_OPTION) {
            dirImagenFondo = jf.getSelectedFile();
            icono = new ImageIcon(dirImagenFondo.toString());
            lblPrincipal.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
        }
    }//GEN-LAST:event_btnCargarImagenPrincipalActionPerformed

    private void btnCargarImagenInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarImagenInicioActionPerformed
        JFileChooser jf = new JFileChooser();
        ImagePreviewPanel miniatura = new ImagePreviewPanel();
        jf.setAccessory(miniatura);
        jf.addPropertyChangeListener(miniatura);
        jf.setFileFilter(filtroImagen);
        int opc = jf.showOpenDialog(this);
        if (opc == JFileChooser.APPROVE_OPTION) {
            dirImagenInicio = jf.getSelectedFile();
            ImageIcon icon = new ImageIcon(dirImagenInicio.toString());
            lblInicio.setIcon(PonerImagenLabel.loadImage(icon.getImage()));
        }
    }//GEN-LAST:event_btnCargarImagenInicioActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCargarImagenInicio;
    private javax.swing.JButton btnCargarImagenPrincipal;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblInicio;
    private javax.swing.JLabel lblPrincipal;
    private javax.swing.JTextField txtNombrePerfil;
    private javax.swing.JTextField txtTextoBarraInferior;
    // End of variables declaration//GEN-END:variables
    File dirImagenFondo;
    File dirImagenInicio;
    ImageIcon icono;
    Perfil perfil;
    FileNameExtensionFilter filtroImagen;

    private byte[] convierteFoto(File dirImagen) {
        InputStream is = null;
        byte[] buffer = null;
        try {
            is = new FileInputStream(dirImagen);
            buffer = new byte[(int) dirImagen.length()];
            int readers = is.read(buffer);
        } catch (IOException ex) {
            Logger.getLogger(iFrmImgFondo.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(iFrmImgFondo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return buffer;
    }
}
